package com.neuedu.service.impl;

import com.neuedu.common.DBUtil;
import com.neuedu.dao.MenuDao;
import com.neuedu.dao.impl.MenuDaoImpl;
import com.neuedu.factory.ClassFactory;
import com.neuedu.service.MenuService;

import java.sql.Connection;
import java.util.*;

public class MenuServiceImpl implements MenuService {
    @Override
    public List<Map<String, Object>> getAllMenu() {
        MenuDao md = (MenuDao) ClassFactory.createObj("MenuDao");
        Connection con = DBUtil.getCon();
        String sql="SELECT m.menuname,sm.submenuname,sm.path FROM menu m,submenu sm WHERE m.mid=sm.mid";
        List<Map<String, Object>> list = md.getAllMenu(sql);
        List<Map<String,Object>> list1=new ArrayList<>();
        List<Map<String,Object>> slist = new ArrayList<>();
        Map<String,Object> map1=new HashMap<>();
        Map<String,Object> map2;
        //初始化map1
        map1.put("label","");
        Set<Map.Entry<String,Object>> set;
        for (Map<String,Object> map:list) {
            set=map.entrySet();
            Iterator<Map.Entry<String,Object>> it=set.iterator();
            while (it.hasNext()){
                Map.Entry<String,Object> en=it.next();
                String key = en.getKey();
                String value= (String) en.getValue();
                if (key.equals("menuname")&&!map1.get("label").equals(value)){
                    if (!map1.get("label").equals("")){
                        list1.add(map1);
                        map1=new HashMap<>();
                        slist=new ArrayList<>();
                    }
                    map1.put("label",value);
                }
                if (key.equals("submenuname")){
                    map2=new HashMap<>();
                    map2.put("label",value);
                    slist.add(map2);
                    map1.put("chilrden",slist);
                }
                if (key.equals("path")){
                    map2=new HashMap<>();
                    map2.put("label",value);
                    slist.add(map2);
                    map1.put("chilrden",slist);
                }
            }
        }
        //把最后一个map1加进去
        list1.add(map1);
        return list1;
    }

    @Override
    public List<Map<String, Object>> getAllMenu1() {
        MenuDao md = (MenuDao) ClassFactory.createObj("MenuDao");
        //  后端给前端传递数据类型
        String sql="SELECT m.menuname pname,sm.submenuname cname,sm.path,m.icon picon,sm.icon cicon FROM menu m,submenu sm WHERE m.mid=sm.mid ORDER BY sm.mid";
        List<Map<String,Object>> restList = new ArrayList<>();
        Set<Object> isRepeat = new HashSet<>();//判断是否重复
        List<Map<String,Object>> list = md.getAllMenu(sql);
        Map<String,Object> map2=new HashMap<>();
        //  将list转换为前端需要的数据类型
        //  遍历list，输出其中所有的数据
        for(Map<String,Object> map:list) {
            int oId = isRepeat.size();
            isRepeat.add(map.get("pname"));
            if(oId!=isRepeat.size()) {
                Map<String,Object> restMap = new HashMap<>();
                map2=new HashMap<>();
                restMap.put("name", map.get("pname"));
                restMap.put("icon", map.get("picon"));
                List<Object> ls = new ArrayList<>();
                map2.put("name",map.get("cname"));
                map2.put("path",map.get("path"));
                map2.put("icon",map.get("cicon"));
                ls.add(map2);
                restMap.put("children", ls);
                restList.add(restMap);
            }else {
                Map<String,Object> restMap = restList.get(restList.size()-1);
                if(restMap.get("children")!=null) {
                    map2=new HashMap<>();
                    map2.put("name",map.get("cname"));
                    map2.put("path",map.get("path"));
                    map2.put("icon",map.get("cicon"));
                    ((ArrayList<Object>)restMap.get("children")).add(map2);
                }
            }
        }
        return restList;
    }
}
